// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_d3d9_utilities.h"

namespace MK3DE
{

	static D3DRENDERSTATETYPE RenderStateMap[] = {
		D3DRS_ALPHABLENDENABLE,
		D3DRS_ALPHATESTENABLE,
		D3DRS_ZENABLE,
		D3DRS_ZWRITEENABLE,
		D3DRS_CULLMODE,
		D3DRS_SRCBLEND,
		D3DRS_DESTBLEND,
		D3DRS_ALPHAFUNC,
		D3DRS_ALPHAREF,
	};

	static DWORD CullingModeMap[] = {
		D3DCULL_NONE,
		D3DCULL_CW,
		D3DCULL_CCW,
	};

	static DWORD BlendParamMap[] = {
		D3DBLEND_SRCALPHA,
		D3DBLEND_INVSRCALPHA,
		D3DBLEND_ONE,
	};

	static DWORD AlphaTestType[] = {
		D3DCMP_NEVER,
		D3DCMP_LESS,
		D3DCMP_EQUAL,
		D3DCMP_NOTEQUAL,
		D3DCMP_LESSEQUAL,
		D3DCMP_GREATER,
		D3DCMP_GREATEREQUAL,
		D3DCMP_ALWAYS,
	};

	static DWORD* RenderStateValueMap[] = {
		NULL,																// D3DRS_ALPHABLENDENABLE
		NULL,																// D3DRS_ALPHATESTENABLE
		NULL,																// D3DRS_ZENABLE
		NULL,																// D3DRS_ZWRITEENABLE
		CullingModeMap,											// D3DRS_CULLMODE
		BlendParamMap,											// D3DRS_SRCBLEND
		BlendParamMap,											// D3DRS_DESTBLEND
		AlphaTestType,												// D3DRS_ALPHAFUNC
		NULL,																// D3DRS_ALPHAREF
	};

	D3DFORMAT FormatMapping(PIXEL_FORMAT format)
	{
		switch (format)
		{
		case PF_R5G6B5:
			return D3DFMT_R5G6B5;

		case PF_A4R4G4B4:
			return D3DFMT_A4R4G4B4;

		case PF_X8R8G8B8:
			return D3DFMT_X8R8G8B8;

		case PF_A8R8G8B8:
			return D3DFMT_A8R8G8B8;

		case PF_R32F:
			return D3DFMT_R32F;

		case PF_A8:
			return D3DFMT_A8;

		case PF_L8:
			return D3DFMT_L8;

		default:
			return D3DFMT_UNKNOWN;
		}
	}

	PIXEL_FORMAT FormatMapping(D3DFORMAT format)
	{
		switch (format)
		{
		case D3DFMT_R5G6B5:
			return PF_R5G6B5;

		case D3DFMT_A4R4G4B4:
			return PF_A4R4G4B4;

		case D3DFMT_X8R8G8B8:
			return PF_X8R8G8B8;

		case D3DFMT_A8R8G8B8:
			return PF_A8R8G8B8;

		case D3DFMT_R32F:
			return PF_R32F;

		case D3DFMT_A8:
			return PF_A8;

		case D3DFMT_L8:
			return PF_L8;

		default:
			return PF_UNKNOWN;
		}
	}

	D3DRENDERSTATETYPE RenderStateMapping(DWORD renderState)
	{
		return RenderStateMap[renderState];
	}

	DWORD RenderStateValueMapping(DWORD renderState, DWORD value)
	{
		DWORD* valueMap = RenderStateValueMap[renderState];
		if (valueMap)
			return valueMap[value];
		return value;
	}
}